package com.fpno3.dao.junit;

import com.fpno.bean.Customer;
import com.fpno3.dao.CustomerDAOImpl;
import com.fpno4.util.JDBCUtils;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

class CustomerDAOImplTest {
    private CustomerDAOImpl dao=new CustomerDAOImpl();
    @Test
    void insert() {
        Connection connection=null;
        try {
        connection = JDBCUtils.getConnection();
        Customer customer = new Customer(1, "yuxiaodei", "xiaohui@qq.com", new Date(6238463875682L));
        dao.insert(connection,customer);
        System.out.println("添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        JDBCUtils.closeResource(connection,null);

        }
    }

    @Test
    void deleteById() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection();
            dao.deleteById(connection,23);
            System.out.println("");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }


    @Test
    void update() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection();
            Customer customer = new Customer(22,"fan","fan@qq.com",new Date(4273947294L));
            dao.update(connection,customer);
            System.out.println("");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }

    @Test
    void getCustomerById() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection3();
            Customer customer = dao.getCustomerById(connection, 12);
            System.out.println(customer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }

    @Test
    void getAll() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection();
            List<Customer> list = dao.getAll(connection);
            list.forEach(System.out::println);
            System.out.println("");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }

    @Test
    void getCount() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection();
            Long count = dao.getCount(connection);
            System.out.println(count);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }

    @Test
    void getMaxBirth() {
        Connection connection=null;
        try {
            connection = JDBCUtils.getConnection();
            Date maxBirth = dao.getMaxBirth(connection);
            System.out.println(maxBirth);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);

        }
    }
}